﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Common.Utilities;
using Common.Utilities.Dispatcher;

namespace Workflows.Components.TextMining.EnumerationObjects
{
    /// <summary>
    /// 
    /// </summary>
    public partial class PdfDocListEditor : Form, IComponentEditor
    {
        #region fields
        private object _ActivityNode;
        private PdfDocListComponent _DocListComponent;
        #endregion

        #region init
        /// <summary>
        /// 
        /// </summary>
        public PdfDocListEditor()
        {
            InitializeComponent();
        }

        private void PdfDocListEditor_Load(object sender, EventArgs e)
        {

        }
        #endregion

        public event UpdateComponentSettings UpdateComponentSettingEvent;

        public object OwnerActivityNode
        {
            get { return _ActivityNode; }
            set { _ActivityNode=value; }
        }

        public Type WorkflowComponentType
        {
            get { return typeof(PdfDocListComponent); }
        }

        public void LoadSettings(GlobalVariable[] globalVars, WorkflowComponentBase[] fromComponents,
                                 object activityNode, WorkflowComponentBase wfComponent)
        {
            this._ActivityNode = activityNode;
            this._DocListComponent = (PdfDocListComponent) wfComponent;

            this.txt_ConnStr.Text = this._DocListComponent.ConnStr;
            this.rt_SQL.Text = this._DocListComponent.SelectSqlCommand;
            this.cbo_DocIDField.Items.Clear();
            this.cbo_FileNameField.Items.Clear();
            this.cbo_BFileField.Items.Clear();
            if(!string.IsNullOrEmpty(this._DocListComponent.ConnStr) && !string.IsNullOrEmpty(this._DocListComponent.SelectSqlCommand))
            {
                TableColumn[] cols = DbUtil.GetTableColumns(DataProviderType.MSSQL, this._DocListComponent.ConnStr,
                                                            this._DocListComponent.SelectSqlCommand);
                if(cols !=null && cols.Length>0)
                {
                    foreach(TableColumn col in cols)
                    {
                        this.cbo_DocIDField.Items.Add(col.ColumnName);
                        this.cbo_FileNameField.Items.Add(col.ColumnName);
                        this.cbo_BFileField.Items.Add(col.ColumnName);
                    }
                }
            }
            if(!string.IsNullOrEmpty(this._DocListComponent.DocIDField) && this.cbo_DocIDField.Items.Count>0)
            {
                for(int i=0;i<this.cbo_DocIDField.Items.Count;i++)
                {
                    if(this.cbo_DocIDField.Items[i].ToString()==this._DocListComponent.DocIDField)
                    {
                        this.cbo_DocIDField.SelectedIndex = i;
                        break;
                    }
                }
            }
            if (!string.IsNullOrEmpty(this._DocListComponent.FileNameField) && this.cbo_FileNameField.Items.Count > 0)
            {
                for (int i = 0; i < this.cbo_FileNameField.Items.Count; i++)
                {
                    if (this.cbo_FileNameField.Items[i].ToString() == this._DocListComponent.FileNameField)
                    {
                        this.cbo_FileNameField.SelectedIndex = i;
                        break;
                    }
                }
            }
            if (!string.IsNullOrEmpty(this._DocListComponent.BFileFieldName) && this.cbo_BFileField.Items.Count > 0)
            {
                for (int i = 0; i < this.cbo_BFileField.Items.Count; i++)
                {
                    if (this.cbo_BFileField.Items[i].ToString() == this._DocListComponent.BFileFieldName)
                    {
                        this.cbo_BFileField.SelectedIndex = i;
                        break;
                    }
                }
            }

            this.txt_Format.Text = this._DocListComponent.FileNameFormat;
            this.txt_LocalFolderPath.Text = this._DocListComponent.LocalFolderPath;
            this.txt_FileFilter.Text = this._DocListComponent.FileFilter;
            this.ck_IncludeSubFolder.Checked = this._DocListComponent.IncludeSubfolders;
            this.txt_ResultFilePath.Text = this._DocListComponent.ResultFilePath;
        }

        public void SaveSettings()
        {
            this._DocListComponent.ConnStr = this.txt_ConnStr.Text;
            this._DocListComponent.SelectSqlCommand = this.rt_SQL.Text;
            this._DocListComponent.DocIDField = this.cbo_DocIDField.Text;
            this._DocListComponent.FileNameField = this.cbo_FileNameField.Text;
            this._DocListComponent.BFileFieldName = this.cbo_BFileField.Text;
            this._DocListComponent.FileNameFormat = this.txt_Format.Text;
            this._DocListComponent.LocalFolderPath = this.txt_LocalFolderPath.Text;
            this._DocListComponent.FileFilter = this.txt_FileFilter.Text;
            this._DocListComponent.IncludeSubfolders = this.ck_IncludeSubFolder.Checked;
            this._DocListComponent.ResultFilePath = this.txt_ResultFilePath.Text;
        }

        public TestResult Test()
        {
            this.SaveSettings();
            return new TestResult(true,"Success");
        }

        private void tx_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog dlg=new FolderBrowserDialog();
            if(dlg.ShowDialog()==System.Windows.Forms.DialogResult.OK)
            {
                this.txt_LocalFolderPath.Text = dlg.SelectedPath;
            }
        }

        private void btn_Refresh_Click(object sender, EventArgs e)
        {
            if(!string.IsNullOrEmpty(this.txt_ConnStr.Text) && !string.IsNullOrEmpty(this.rt_SQL.Text))
            {
                this.cbo_DocIDField.Items.Clear();
                this.cbo_FileNameField.Items.Clear();
                this.cbo_BFileField.Items.Clear();
                TableColumn[] cols =
                    DbUtil.GetTableColumns(
                        DataProviderType.MSSQL,
                        this.txt_ConnStr.Text,
                        this.rt_SQL.Text);
                if (cols != null && cols.Length > 0)
                {
                    foreach (TableColumn col in cols)
                    {
                        this.cbo_DocIDField.Items.Add(col.ColumnName);
                        this.cbo_FileNameField.Items.Add(col.ColumnName);
                        this.cbo_BFileField.Items.Add(col.ColumnName);
                    }
                }
                if (!string.IsNullOrEmpty(this._DocListComponent.DocIDField) && this.cbo_DocIDField.Items.Count > 0)
                {
                    for (int i = 0; i < this.cbo_DocIDField.Items.Count; i++)
                    {
                        if (this.cbo_DocIDField.Items[i].ToString() == this._DocListComponent.DocIDField)
                        {
                            this.cbo_DocIDField.SelectedIndex = i;
                            break;
                        }
                    }
                }
                if (!string.IsNullOrEmpty(this._DocListComponent.FileNameField) && this.cbo_FileNameField.Items.Count > 0)
                {
                    for (int i = 0; i < this.cbo_FileNameField.Items.Count; i++)
                    {
                        if (this.cbo_FileNameField.Items[i].ToString() == this._DocListComponent.FileNameField)
                        {
                            this.cbo_FileNameField.SelectedIndex = i;
                            break;
                        }
                    }
                }
                if (!string.IsNullOrEmpty(this._DocListComponent.BFileFieldName) && this.cbo_BFileField.Items.Count > 0)
                {
                    for (int i = 0; i < this.cbo_BFileField.Items.Count; i++)
                    {
                        if (this.cbo_BFileField.Items[i].ToString() == this._DocListComponent.BFileFieldName)
                        {
                            this.cbo_BFileField.SelectedIndex = i;
                            break;
                        }
                    }
                }
            }
        }

        private void btn_OK_Click(object sender, EventArgs e)
        {
            this.SaveSettings();
            if(this.UpdateComponentSettingEvent !=null)
            {
                this.UpdateComponentSettingEvent(this._ActivityNode, this._DocListComponent);
            }
            this.Close();
        }

        private void btn_Cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btn_SaveTo_Click(object sender, EventArgs e)
        {
            SaveFileDialog dlg = new SaveFileDialog();
            dlg.Filter = "Xml files(*.xml)|*.xml|All files(*.*)|*.*";
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                this.txt_ResultFilePath.Text = dlg.FileName;
            }
        }

    }
}
